//
//  SCToolBar.swift
//  weibo
//
//  Created by 阿城 on 15/12/14.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit

enum ComposeButtonType: Int{
    case Picture = 0   // 图片
    case Mention = 1   // @
    case Trend = 2     // 话题
    case Emoticon = 3   // 表情
    case Add = 4       // 加
}

class SCComposeBar: UIStackView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    var buttonBackclosure: ((type: ComposeButtonType)->())?

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        
        axis = .Horizontal
        distribution = .FillEqually
        
        //添加子控件
        addButton("compose_toolbar_picture", type: .Picture)
        addButton("compose_mentionbutton_background", type: .Mention)
        addButton("compose_trendbutton_background", type: .Trend)
        addButton("compose_emoticonbutton_background", type: .Emoticon)
        addButton("compose_add_background", type: .Add)
        
        
        
    }
    
    private func addButton(imgName: String, type:ComposeButtonType){
        let btn = UIButton()
        let bgImg = UIImage(named: "compose_toolbar_background")
//        btn.backgroundColor = UIColor(patternImage: bgImg)
        btn.setBackgroundImage(bgImg, forState: UIControlState.Normal)
        btn.setBackgroundImage(bgImg, forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        btn.tag = type.rawValue
        addArrangedSubview(btn)
        btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    @objc private func buttonClick(button: UIButton){
        //printLog(button.tag)
        let type = ComposeButtonType(rawValue: button.tag)!
        buttonBackclosure?(type: type)
        
        //处理表情按钮
        if type == ComposeButtonType.Emoticon{
            
            let keyBoardName = "compose_keyboardbutton_background"
            let emojiName = "compose_emoticonbutton_background"
            let imageName = button.currentImage == UIImage(named: "\(emojiName)_highlighted") ? keyBoardName : emojiName
            
            button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
            button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
            
        }
    }
    
}
